/*
 * 一个用户可以身兼多个店铺的代理
 */
var skill = {
	_setMA: function(bus_id, MA_id) {
		// this.MA_id = MA_id;
		var MA_map = this.MA_map || (this.MA_map = {});
		if (MA_id) {
			MA_map[bus_id] = MA_id
		} else {
			delete MA_map[bus_id];
		}
		this.save();
	},
	_removeMA: function(bus_id) {
		this.MA_map && delete this.MA_map[bus_id];
		this.save();
	},
	getMA: function() {
		var MA_map = this.MA_map || {}
		Object.keys(MA_map).reduce((res, bus_id) => {
			res[bus_id] = BusMACon.get(MA_map[bus_id])
			return res;
		}, this._MA = {})
		return this._MA;
	},
	isMAByBusId: function(bus_id) {
		var MA_map = this.MA_map || {}
		return MA_map[bus_id];
	},
	getMAByBusId: function(bus_id) {
		var MA_map = this.MA_map || {}
		var MA = MA_map[bus_id] && BusMACon.get(MA_map[bus_id])
		if (!MA) {
			throwCusError("用户不属于指定商家的市代")
		}
		return MA;
	},
	getMAAchListByBusId: function(bus_id, filter) {
		var MA = this.getMAByBusId(bus_id);
		return MA.getAchDetailListByFilter(filter)
	},
	/*
	 * 门店
	 */
	getShopById: function(shop_id) {
		var shop = BusShopCon.get(shop_id);
		if (!shop) {
			throwCusError("找不到指定门店")
		}
		if (!$Object.hasValue(this.MA_map || {}, shop.MA_id)) {
			throwCusError("该门店不属于指定市代")
		}
		return shop;
	},
	getAllShopList: function() {
		var MA_map = this.getMA();
		return Object.keys(MA_map).reduce((res, bus_id) =>
			res.concat(MA_map[bus_id].getShopList()), [])
	},
	getShopListByBusId: function(bus_id) {
		var MA = this.getMAByBusId(bus_id);
		return MA.getShopList();
	},
};
module.exports = skill;

var db = require("../db/index");
var UserCon = require("./_con");
var CardCon = require("../card/_con");
var UserCartGoodsCon = require("../user.cart_goods/_con");
var UserAddressCon = require("../user_address/_con");
var BusCon = require("../bus/_con");
var BusGuideCon = require("../bus.guide/_con");
var BusMACon = require("../bus.MA/_con");
var BusShopCon = require("../bus.shop/_con");
var GoodsCon = require("../goods/_con");
var AchCon = require("../ach/_con");
var PayAchCon = require("../pay_ach/_con");
var CommentCon = require("../comment/_con");
var EvaluationCon = require("../eval/_con");
var BuffCon = require("../buff/_con");
var BusUserBindCon = require("../bus_user_bind/_con");
var config = require("../_include/config");
var data_cut = require("../_include/data_cut");
var $Array = require("../_include/$.Array");
var $Object = require("../_include/$.Object");
var md5 = require("../_include/md5");
var helper = require("../_include/helper");
var Fiber = require('fibers');